package com.plian.system.service.sys.fun;

import com.plian.Tools.Tools;
import com.plian.Tools.TreeUtli;
import com.plian.Tools.UuidUtil;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.dao.sys.fun.FunDao;
import com.plian.system.model.CommonCode;
import com.plian.system.model.ListResult;
import com.plian.system.model.ObjResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class FunService {
    @Autowired
    FunDao funDao;
    /**
     * 保存
     * @param pd
     * @return
     */
    public ObjResult save(PageData pd) {
        ObjResult objResult;
        try {
            if(Tools.isEmpty(pd.getString("parentId"))){
                pd.put("parentId","0");
            }
            pd.put("id", UuidUtil.get32UUID());
            funDao.save(pd);
            objResult= new ObjResult(CommonCode.SUCCESS);
        } catch (Exception e) {
            objResult=  new ObjResult(CommonCode.FAIL);
            log.error(ExceptionUtils.getFullStackTrace(e));
        }
        return objResult;
    }
    /**
     * 修改
     * @param pd
     * @return
     */
    public ObjResult edit(PageData pd) {
        ObjResult objResult;
        if(pd.getString("id").equals(pd.getString("parentId"))){
            objResult= new ObjResult(CommonCode.FAIL);
            objResult.setMessage("父id重复!");
        }else{
            try {
                funDao.edit(pd);
                objResult= new ObjResult(CommonCode.SUCCESS);
            } catch (Exception e) {
                objResult=  new ObjResult(CommonCode.FAIL);
                log.error(ExceptionUtils.getFullStackTrace(e));
            }
        }
        return objResult;
    }
    /**
     * 删除
     * @return
     */
    public ObjResult del_ids(List<String> list) {
        ObjResult objResult;
        try {
            funDao.del_ids(list);
            objResult= new ObjResult(CommonCode.SUCCESS);
        } catch (Exception e) {
            objResult=  new ObjResult(CommonCode.FAIL);
            log.error(ExceptionUtils.getFullStackTrace(e));
        }
        return objResult;
    }

    /**
     * 根据id查询
     * @param p
     * @throws Exception
     */
    public ObjResult findbyid(PageData p) {
        ObjResult objResult;
        try {
            PageData p_user=funDao.findbyid(p.getString("id"));
            objResult=new ObjResult(CommonCode.SUCCESS);
            objResult.setPageData(p_user);
        }catch (Exception e) {
            objResult=new ObjResult(CommonCode.ERROR);
            log.error(ExceptionUtils.getFullStackTrace(e));
            return objResult;
        }
        return objResult;
    }

    /**
     *获取权限树
     */
    public ListResult funZtree(PageData pd){
        ListResult listResult;
        try {
            List<PageData> listAll=funDao.list(pd);
            TreeUtli treeUtli=new TreeUtli();
            List<PageData> list_tree=treeUtli.getChlid("0",listAll);
            listResult=new ListResult(CommonCode.SUCCESS);
            listResult.setList(list_tree);
        } catch (Exception e) {
            listResult=new ListResult(CommonCode.FAIL);
            log.error(ExceptionUtils.getFullStackTrace(e));
        }
        return listResult;
    }

    /**
     * 批量调整
     * @param p
     * @throws Exception
     */
    public ObjResult adjustment(PageData p) {
        ObjResult objResult;
        try {
            List<String> list= (List<String>) p.get("ids");
            String parentId=p.getString("parentId");
            if(Tools.isEmpty(parentId)){
                parentId="0";
            }
            boolean isrepeat=true;
            for (String s : list) {
                if (s.equals(parentId)) {
                    isrepeat=false;
                    break;
                }
            }
            if(isrepeat){
                funDao.adjustment(p);
                objResult=new ObjResult(CommonCode.SUCCESS);
            }else{
                objResult=new ObjResult(CommonCode.ERROR);
                objResult.setMessage("父级重复!!!");
            }
        }catch (Exception e) {
            objResult=new ObjResult(CommonCode.ERROR);
            log.error(ExceptionUtils.getFullStackTrace(e));
            return objResult;
        }
        return objResult;
    }

    //TODO DEL
//    public ListResult<PageData> getFormType(){
//        ListResult<PageData> listResult=new ListResult<>(CommonCode.SUCCESS);
//        List<PageData> formTypes = funDao.getByFormType();
//        formTypes.forEach(record->{
//            String name=record.getString("name");
//            record.put("name",name.replaceAll("-新增",""));
//        });
//        listResult.setList(formTypes);
//        listResult.setTotal(formTypes.size());
//        return listResult;
//    }
}
